#include "proj.h"



boolean Net_FlgHallSensLst1;
boolean Net_FlgHallSensLst2;
boolean Net_FlgHallSensLst3;
boolean Net_FlgToggHallSensTrig;

boolean Net_FlgHallSensSpdRdy[3];
uint32 Net_TickHallSens[3];
uint32 Net_TickHallSensLst[3];
uint32 Net_TickDif[3];

void HallSensInt_init()
{

	Net_FlgToggHallSensTrig = 0;

	Net_FlgHallSensSpdRdy[0] = false;
	Net_FlgHallSensSpdRdy[1] = false;
	Net_FlgHallSensSpdRdy[2] = false;
	Net_TickHallSens[0] = (uint32)(-1);
	Net_TickHallSens[1] = (uint32)(-1);
	Net_TickHallSens[2] = (uint32)(-1);
	Net_TickHallSensLst[0] = (uint32)(-1);
	Net_TickHallSensLst[1] = (uint32)(-1);
	Net_TickHallSensLst[2] = (uint32)(-1);
	Net_TickDif[0] = (uint32)(-1);
	Net_TickDif[1] = (uint32)(-1);
	Net_TickDif[2] = (uint32)(-1);
}

static __inline void ProcSens(uint8 ind)
{
	Net_TickHallSensLst[ind] = Net_TickHallSens[ind];
	Net_TickHallSens[ind] = SysTick->VAL;

	if (!Net_FlgHallSensSpdRdy[ind] && Net_TickHallSensLst[ind] != (uint32)(-1))
	{
		Net_FlgHallSensSpdRdy[ind] = true;
	}

	if (Net_FlgHallSensSpdRdy[ind])
	{
		if (Net_TickHallSensLst[ind] <= Net_TickHallSens[ind])
		{
			Net_TickDif[ind] = Net_TickHallSensLst[ind] + 0x1000000u - Net_TickHallSens[ind];
		}
		else
		{
			Net_TickDif[ind] = Net_TickHallSensLst[ind] - Net_TickHallSens[ind];
		}
	}
}

void HallSensInt_trig()
{
	boolean _loc_bool = false;

	Net_FlgHallSensLst1 = Net_FlgHallSens1;
	Net_FlgHallSens1 = (boolean)GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6);
	Net_FlgHallSensLst2 = Net_FlgHallSens2;
	Net_FlgHallSens2 = (boolean)GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7);
	Net_FlgHallSensLst3 = Net_FlgHallSens3;
	Net_FlgHallSens3 = (boolean)GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8);

	/* Nur steigende Flanke */
	if (!Net_FlgHallSensLst1 && Net_FlgHallSens1)
	{
		ProcSens(0);
		_loc_bool = true;
	}

	if (!Net_FlgHallSensLst2 && Net_FlgHallSens2)
	{
		ProcSens(1);
		_loc_bool = true;
	}

	if (!Net_FlgHallSensLst3 && Net_FlgHallSens3)
	{
		ProcSens(2);
		_loc_bool = true;
	}

	if (_loc_bool)
	{
		Net_FlgToggHallSensTrig = !Net_FlgToggHallSensTrig;
	}
}
